package main

import (
	"carSpider/data"
	"carSpider/response"
	"github.com/Flyits/goSpider/src"
	"strings"
)

func main() {
	index := goSpider.UrlItem{
		Url:         "https://car.yiche.com/",
		HandlerFunc: response.Brand,
	}
	//index := goSpider.UrlItem{
	//	Url:         "https://car.m.yiche.com/benchiglsjijinkou/m145917/peizhi",
	//	HandlerFunc: response.Config,
	//	SpiderFunc:  goSpider.JsLoadRequest,
	//	WaitExpr:    "#content-iscoll",
	//	Expr:        "#content-iscoll",
	//	Attach:      map[string]int{"versionId": 53482},
	//}

	spider := goSpider.Spider{}

	spider.Init()
	data.Init(&spider)
	//version(&spider)
	spider.AddJob(index)
	_ = index
	spider.Wg.Wait()
}

func version(spider *goSpider.Spider) {
	var versions []data.Version
	data.Db.Where("id>53455").Select("id,first_image").Find(&versions)
	for k, _ := range versions {
		urlParams := strings.Split(strings.Trim(versions[k].FirstImage, "/"), "/")
		attach := map[string]int{"versionId": versions[k].ID}
		spider.AddJob(goSpider.UrlItem{
			Url:         response.BaseUrl + versions[k].FirstImage,
			HandlerFunc: response.Config,
			SpiderFunc:  goSpider.JsLoadRequest,
			WaitExpr:    "#content-iscoll",
			Expr:        "#content-iscoll",
			Attach:      attach,
		})
		spider.AddJob(goSpider.UrlItem{
			Url:         response.ImgBaseUrl + strings.TrimLeft(urlParams[1], "m"),
			HandlerFunc: response.Image,
			Attach:      attach,
		})
	}
}
